﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Studio77.CrazyJigsaw.Entities;
using System.IO.IsolatedStorage;

namespace Studio77.CrazyJigsaw.Core
{
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        /// <summary>
        /// 关卡数
        /// </summary>
        public const int ClassStageCount = 15;

        private StageManager()
        {

        }

        //keys
        private const string STAGE_DATA_KEY = "stageDataKey";
        private const string LAST_STAGE_INDEX_KEY = "lastStageIndexKey";
        private const string IS_PLAY_SOUND_KEY = "isPlaySoundKey";

        /// <summary>
        /// 存储游戏关卡数据
        /// key: StageType + Index
        /// </summary>
        public Dictionary<string, StageData> StageDatas { get; set; }

        public string CurrentStageType { get; set; }

        private bool isPlaySound = true;
        /// <summary>
        /// 
        /// </summary>
        public bool IsPlaySound
        {
            get
            {
                return isPlaySound;
            }
            set
            {
                isPlaySound = value;
                IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY] = isPlaySound;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private string lastStage;
        public string LastStage
        {
            get
            {
                return lastStage;
            }
            set
            {
                lastStage = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_INDEX_KEY] = lastStage;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        public void Init()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_STAGE_INDEX_KEY))
            {
                lastStage = (string)IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_INDEX_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IS_PLAY_SOUND_KEY))
            {
                isPlaySound = (bool)IsolatedStorageSettings.ApplicationSettings[IS_PLAY_SOUND_KEY];
            }
            if (IsolatedStorageSettings.ApplicationSettings.Contains(STAGE_DATA_KEY))
            {
                StageDatas = IsolatedStorageSettings.ApplicationSettings[STAGE_DATA_KEY] as Dictionary<string, StageData>;
            }
            else
            {
                // 初始化
                StageDatas = new Dictionary<string, StageData>();
            }

        }

        public void SaveStageDatas()
        {
            IsolatedStorageSettings.ApplicationSettings[STAGE_DATA_KEY] = StageDatas;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        public StageData GetCurrentStageData()
        {
            if (StageDatas.ContainsKey(lastStage))
            {
                return StageDatas[lastStage];
            }
            else
            {
                return new StageData();
            }
        }

    }
}
